//
//  ChoseCoinController.m
//  SafPay
//
//  Created by 贺庆博 on 2018/6/19.
//  Copyright © 2018年 ebo. All rights reserved.
//

#import "ChoseCoinController.h"
#import "ChoseCoinCell.h"
#import "TDNoDataView.h"
@interface ChoseCoinController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *arrDS;
@property (strong,nonatomic) TDNoDataView *dataView;
@end

@implementation ChoseCoinController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initData];
}
- (void)initData{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:[AccountTools shared].currentAccount.token forKey:@"token"];
    [dic setValue:[AccountTools shared].currentAccount.userId forKey:@"uid"];


    [[TRRequest request] Post:kSetURL(kCoinList) parameters:dic success:^(TRRequest *request, NSDictionary *responseDict) {
        
        if ([[responseDict valueForKey:@"error"] integerValue] == 0) {
            [self.arrDS removeAllObjects];
            [[responseDict valueForKey:@"data"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                CoinListModel *model = [CoinListModel new];
//                [model setValuesForKeysWithDictionary:obj];
                [model yy_modelSetWithDictionary:obj];
                
                if(self.isAdd==YES){
                    
                    if([self isHave:model.name]==NO){
                        
                        [self.arrDS addObject:model];
                    }
                  
                    
                }else{
                    
                    [self.arrDS addObject:model];
                    
                }
                
              
            }];
            self.arrDS.count==0?[self.view addSubview:self.dataView]:[self.dataView removeFromSuperview];
            [self.tableView reloadData];
        }else
        {
            [MBProgressHUD showErrorWithText:[responseDict valueForKey:@"error_msg"]];
        }
    } failure:^(TRRequest *request, NSError *error) {
         self.arrDS.count==0?[self.view addSubview:self.dataView]:[self.dataView removeFromSuperview];
        [MBProgressHUD showErrorWithText:@"网络请求失败！"];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"币种选择";
    if(self.isAdd==YES){
        
        self.navigationItem.title = @"添加币种";
    }
    [self initUI];
    // Do any additional setup after loading the view.
}
- (void)initUI
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(18, 16, 29, 16));
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.layer.cornerRadius = 3;
        _tableView.delegate = self;
        _tableView.dataSource = self;
       // [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_tableView registerNib:[UINib nibWithNibName:@"ChoseCoinCell" bundle:nil] forCellReuseIdentifier:@"ChoseCoinCell"];
    }
    return _tableView;
}
- (TDNoDataView *)dataView{
    if (!_dataView) {
        _dataView = [[TDNoDataView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) img:kImageName(@"td_nohistory") title:@"已添加所有币种"];
        
      
    }
    return _dataView;
}
- (NSMutableArray *)arrDS
{
    if (!_arrDS) {
        _arrDS = [NSMutableArray array];
    }
    return _arrDS;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrDS.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChoseCoinCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChoseCoinCell"];
    cell.model = self.arrDS[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CoinListModel *model = self.arrDS[indexPath.row];
    if (self.coinClickBlock) {
        self.coinClickBlock(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(BOOL)isHave:(NSString *)name{
    
    __block BOOL has;
    
    [self.isHaveCoinArr enumerateObjectsUsingBlock:^(WalletCoinListModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        if([name isEqualToString:obj.coin_name]){
            has=YES;
        }
        
    }];
    return has;
}
@end
